#!/bin/bash
# Copyright (c) 2025 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


# Build the project and package into "dist/"

source "$(dirname "$0")/common_lib.sh"

copy-runtime() {
    echo "Copying the runtime files"
    cp -r runtime/include/* "$INCLUDE_DIR"
    cp -r runtime/src/* "$SRC_DIR"
}

build-compiler() {
    echo "Building the compiler"
    local compiler_dir="$LIB_DIR/compiler"
    pushd compiler

    ./generate-grammar && poetry build
    cd dist
    tar xf taihe-*.gz
    echo "Copying the compiler"
    mkdir -p "$compiler_dir/taihe" && cp -r taihe-*/taihe "$compiler_dir"

    site_packages_dir=$(python3 -c "import site; print(site.getsitepackages()[0])")
    mkdir -p "$compiler_dir/lib"
    cp -r "$site_packages_dir/antlr4" "$compiler_dir/lib/"
    cp -r "$site_packages_dir/typing_extensions.py" "$compiler_dir/lib/"

    popd 2>/dev/null

    echo "Copying the compiler wrapper"

    local prog="$BIN_DIR/taihec"
    cat >"$prog" <<'EOF'
#!/bin/bash -eu

export TAIHE_ROOT="$(realpath $(dirname "$0")/..)"
export PYTHONPATH="${TAIHE_ROOT}/lib/taihe/compiler:${TAIHE_ROOT}/lib/taihe/compiler/lib:${PYTHONPATH:-}"

exec python3 -m taihe.cli.compiler "$@"
EOF
    chmod 0755 "$prog"

    local prog="$BIN_DIR/taihe-tryit"
    cat >"$prog" <<'EOF'
#!/bin/bash -eu

export TAIHE_ROOT="$(realpath $(dirname "$0")/..)"
export PYTHONPATH="${TAIHE_ROOT}/lib/taihe/compiler:${TAIHE_ROOT}/lib/taihe/compiler/lib:${PYTHONPATH:-}"

exec python3 -c 'from taihe.cli.tryit import *; main(BuildConfig(for_distribution=True));' "$@"
EOF
    chmod 0755 "$prog"

}

copy-documentation() {
    cp -r cookbook "$DOC_DIR"
}

write-version() {
    local GIT_COMMIT=$(git rev-parse HEAD)
    local GIT_MESSAGE=$(git log -1 --pretty=%B | head -n 1)
    local BUILD_DATE=$(date +"%Y-%m-%d %H:%M:%S")
    {
        echo "Version       : $VERSION"
        echo "Last Commit   : $GIT_COMMIT"
        echo "Commit Message: $GIT_MESSAGE"
        echo "Build Date    : $BUILD_DATE"
    } > "$DIST/version.txt"
}

main() {
    init_shell_options
    chdir_root

    # Global variable
    VERSION=$(git describe --tags --abbrev=0 2>/dev/null || echo "TRUNK-$(git rev-parse --short=8 HEAD)")
    echo "Version $VERSION"

    echo "Setting up the directories"
    DIST="$PWD/dist/taihe"

    LIB_DIR="$DIST/lib/taihe"
    BIN_DIR="$DIST/bin"
    INCLUDE_DIR="$DIST/include"
    SRC_DIR="$DIST/src/taihe/runtime"
    DOC_DIR="$DIST/share/doc/taihe"
    mkdir -p "$DIST" "$LIB_DIR" "$BIN_DIR" "$INCLUDE_DIR" "$SRC_DIR" "$DOC_DIR"

    # Do the job
    copy-runtime
    copy-documentation
    write-version
    build-compiler
    tar -z -c -f "taihe-$VERSION-$(date +"%Y%m%d").tar.gz" -C "$DIST/.." .
}

main
